package wx.utils;

import com.jinyou.utils.common.DateUtils;
import com.jinyou.utils.common.JYFileUtils;
import com.jinyou.utils.common.ValidateUtil;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.codehaus.xfire.util.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import o2o.system.data.SYS_CONFIG_PATH;
import wx.dao.AccessToken;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import java.io.*;
import java.net.ConnectException;
import java.net.URL;
import java.security.AlgorithmParameters;
import java.security.Security;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 公众平台通用接口工具类
 */
public class WeixinUtil {
    private static Logger log = LoggerFactory.getLogger(WeixinUtil.class);

    /**
     * 发起https请求并获取结果
     *
     * @param requestUrl    请求地址
     * @param requestMethod 请求方式（GET、POST）
     * @param outputStr     提交的数据
     * @return JSONObject(通过JSONObject.get ( key)的方式获取json对象的属性值)
     */
    public static JSONObject httpRequest(String requestUrl, String requestMethod, String outputStr) {
        JSONObject jsonObject = null;
        StringBuffer buffer = new StringBuffer();
        try {
            // 创建SSLContext对象，并使用我们指定的信任管理器初始化
            TrustManager[] tm = {new MyX509TrustManager()};
            SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
            sslContext.init(null, tm, new java.security.SecureRandom());
            // 从上述SSLContext对象中得到SSLSocketFactory对象
            SSLSocketFactory ssf = sslContext.getSocketFactory();

            URL url = new URL(requestUrl);
            HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
            httpUrlConn.setSSLSocketFactory(ssf);

            httpUrlConn.setDoOutput(true);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);
            // 设置请求方式（GET/POST）
            httpUrlConn.setRequestMethod(requestMethod);

            if ("GET".equalsIgnoreCase(requestMethod))
                httpUrlConn.connect();

            // 当有数据需要提交时
            if (null != outputStr) {
                OutputStream outputStream = httpUrlConn.getOutputStream();
                // 注意编码格式，防止中文乱码
                outputStream.write(outputStr.getBytes("UTF-8"));
                outputStream.close();
            }

            // 将返回的输入流转换成字符串
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            bufferedReader.close();
            inputStreamReader.close();
            // 释放资源
            inputStream.close();
            inputStream = null;
            httpUrlConn.disconnect();
            jsonObject = JSONObject.fromObject(buffer.toString());
        } catch (ConnectException ce) {
            log.error("Weixin server connection timed out.");
        } catch (Exception e) {
            log.error("https request error:{}", e);
        }
        return jsonObject;
    }


    /**
     * 发起https请求并获取结果
     *
     * @param requestUrl 请求地址
     * @param map        提交的数据
     * @return JSONObject(通过JSONObject.get ( key)的方式获取json对象的属性值)
     */
    public static void httpRequest3(String requestUrl, Map map, String filePath) {
        RestTemplate rest = new RestTemplate();
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
            HttpEntity requestEntity = new HttpEntity(map, headers);
            ResponseEntity<byte[]> entity = rest.exchange(requestUrl, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);

            byte[] result = entity.getBody();

            inputStream = new ByteArrayInputStream(result);

            File file = new File(filePath);
            if (!file.exists()) {
                file.createNewFile();
            }
            outputStream = new FileOutputStream(file);
            int len = 0;
            byte[] buf = new byte[1024];
            while ((len = inputStream.read(buf, 0, 1024)) != -1) {
                outputStream.write(buf, 0, len);
            }
            outputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    // 获取access_token的接口地址（GET） 限200（次/天）
    public final static String access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
    public final static String code_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECTURI&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
    public final static String open_id_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=APPSECRET&code=CODE&grant_type=authorization_code";
    public final static String open_id_url_little = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=APPSECRET&js_code=CODE&grant_type=authorization_code";
    public final static String gzh_rqcode_url_forever = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=ACCESS_TOKEN";
    public final static String gzh_template_msg_url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
    public final static String xcx_template_msg_url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=ACCESS_TOKEN"; // 小程序模板消息(废弃)

    public final static String xcx_subscribe_msg_url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=ACCESS_TOKEN"; // 小程序订阅消息

    public final static String gzh_create_menu_url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
    public final static String gzh_delete_menu_url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN";
    public final static String gzh_get_user_info = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPEN_ID&lang=zh_CN";
    public final static String xcx_rq_code_url_temp = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN";

    public final static String gzh_get_ticket = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";

    /**
     * 获取access_token
     *
     * @param appid     凭证
     * @param appsecret 密钥
     * @return
     */
    public static AccessToken getAccessToken(String appid, String appsecret) {
        AccessToken accessToken = null;

        String requestUrl = access_token_url.replace("APPID", appid).replace("APPSECRET", appsecret);
        JSONObject jsonObject = httpRequest(requestUrl, "GET", null);
        // 如果请求成功
        if (null != jsonObject) {
            try {
                accessToken = new AccessToken();
                accessToken.setToken(jsonObject.getString("access_token"));
                accessToken.setExpiresIn(jsonObject.getInt("expires_in"));
            } catch (JSONException e) {
                accessToken = null;
                // 获取token失败
                System.out.println("获取token失败 errcode:" + jsonObject.getInt("errcode") + " errmsg:" + jsonObject.getString("errmsg"));
            }
        }
        return accessToken;
    }


    //通过code获取用户的openId
    public static String getUserOpenID(String appId, String appSecret, String code) {
//        AccessToken accessToken = null;

        String openId = null;
        String requestUrl = open_id_url.replace("APPID", appId).replace("APPSECRET", appSecret).replace("CODE", code);
        JSONObject jsonObject = httpRequest(requestUrl, "GET", null);
        // 如果请求成功
        if (null != jsonObject) {
            try {
//                accessToken = new AccessToken();
//                accessToken.setToken(jsonObject.getString("access_token"));
//                accessToken.setExpiresIn(jsonObject.getInt("expires_in"));
//                accessToken.setOpenid(jsonObject.getString("openid"));
//                accessToken.setScope(jsonObject.getString("scope"));

                openId = jsonObject.getString("openid");
            } catch (JSONException e) {
                e.printStackTrace();
//                accessToken = null;
                openId = null;
                // 获取token失败
                System.out.println("获取token失败 " + jsonObject.getInt("errcode") + ",message:" + jsonObject.getString("errmsg"));
            }
        }
        return openId;
    }


    //通过code获取用户的openId
    public static String getUserOpenIDLittle(String appId, String appsecret, String code,
                                             Boolean needSessionKey  // 获取session_key和openId
    ) {
//        AccessToken accessToken = null;


        String openId = null;
        JSONObject jsonObject = null;
        try {
            String requestUrl = open_id_url_little.replace("APPID", appId).replace("APPSECRET", appsecret).replace("CODE", code);
            jsonObject = httpRequest(requestUrl, "GET", null);
            // 如果请求成功
            if (null != jsonObject) {
                try {
//                accessToken = new AccessToken();
//                accessToken.setToken(jsonObject.getString("access_token"));
//                accessToken.setExpiresIn(jsonObject.getInt("expires_in"));
//                accessToken.setOpenid(jsonObject.getString("openid"));
//                accessToken.setScope(jsonObject.getString("scope"));
                    openId = jsonObject.getString("openid");
                } catch (JSONException e) {
//                accessToken = null;
                    openId = null;
                    // 获取token失败
//                    log.error("获取token失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
//                    e.printStackTrace();
                    System.out.println("获取token失败 errcode:" + jsonObject.getInt("errcode") + " errmsg:" + jsonObject.getString("errmsg"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (needSessionKey) {
            return JSONObject.fromObject(jsonObject).toString();
        }
        return openId;

    }

    /**
     * 获取jsApiTicket
     *
     * @param accessToken 凭证
     * @return
     */

    public static JSONObject getJsApiTicket(String accessToken) {
        String requestUrl = gzh_get_ticket.replace("ACCESS_TOKEN", accessToken);
        return httpRequest(requestUrl, "GET", null);
    }


    //-----------------模板消息
    //公众号发送模板消息
    public static Integer sendGzhTemplateMsg(String access_token, String data) {

        Integer code = null;
        try {
            String requestUrl = gzh_template_msg_url.replace("ACCESS_TOKEN", access_token);
            JSONObject jsonObject = httpRequest(requestUrl, "POST", data);
            // 如果请求成功
            if (null != jsonObject) {
                try {
                    code = jsonObject.getInt("errcode");
                } catch (JSONException e) {
                    code = null;
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }

        System.out.println(code);
        return code;
    }

    //小程序发送模板消息
    public static Integer sendXcxTemplateMsg(String access_token, String data) {

//        data = "{\"touser\":\"oH_sH1GhIPnMHlvwDjr6eld80YtY\",\"template_id\":\"yinITaUGOX3chB39wSdXDLGvS2E9m-f9MFLm44SEcJ4\",\"url\":\"http://weixin.qq.com\",\"topcolor\":\"#FF0000\",\"data\":{\"result\":{\"value\":\"黄先生\",\"color\":\"#173177\"},\"withdrawMoney\":{\"value\":\"人民币260.00元\",\"color\":\"#173177\"},\"withdrawTime\":{\"value\":\"人民币260.00元\",\"color\":\"#173177\"},\"cardInfo\":{\"value\":\"0426\",\"color\":\"#173177\"},\"arrivedTime\":{\"value\":\"06月07日19时24分\",\"color\":\"#173177\"},\"remark\":{\"value\":\"消费\",\"color\":\"#173177\"}}}";
//        data = "{\"touser\":\"oH_sH1GhIPnMHlvwDjr6eld80YtY\",\"template_id\":\"yinITaUGOX3chB39wSdXDLGvS2E9m-f9MFLm44SEcJ4\",\"url\":\"http://weixin.qq.com\",\"topcolor\":\"#FF0000\",\"data\":{\"productType\":{\"value\":\"黄先生\",\"color\":\"#173177\"},\"name\":{\"value\":\"人民币260.00元\",\"color\":\"#173177\"},\"number\":{\"value\":\"人民币260.00元\",\"color\":\"#173177\"},\"expDate\":{\"value\":\"06月07日19时24分\",\"color\":\"#173177\"},\"remark\":{\"value\":\"消费\",\"color\":\"#173177\"}}}";
//        String ticket = null;
        Integer code = null;
        try {
            String requestUrl = xcx_subscribe_msg_url.replace("ACCESS_TOKEN", access_token);
            JSONObject jsonObject = httpRequest(requestUrl, "POST", data);
            // 如果请求成功
            if (null != jsonObject) {
                try {
                    code = jsonObject.getInt("errcode");
                } catch (JSONException e) {
                    code = null;
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println(code);
        return code;
    }


    //---------------菜单
    //创建菜单
    public static String createGzhMenu(String access_token, String data) {
//        data = "{\"button\":[{\"type\":\"miniprogram\",\"name\":\"首页\",\"key\":\"shop_home\",\"url\":\"http://baidu.com\",\"appid\":\"wx631237a7c68b1660\",\"pagepath\":\"pages/index/index\"},{\"type\":\"miniprogram\",\"name\":\"我的二维码\",\"key\":\"my_qr_code\",\"url\":\"http://baidu.com\",\"appid\":\"wx631237a7c68b1660\",\"pagepath\":\"pages/share/share\"},{\"name\":\"我的\",\"sub_button\":[{\"type\":\"miniprogram\",\"name\":\"个人中心\",\"key\":\"my_center\",\"url\":\"http://baidu.com\",\"appid\":\"wx631237a7c68b1660\",\"pagepath\":\"pages/user-center/user-center\"},{\"type\":\"miniprogram\",\"name\":\"我的佣金\",\"key\":\"my_yongjin\",\"url\":\"http://baidu.com\",\"appid\":\"wx631237a7c68b1660\",\"pagepath\":\"pages/myYongjin/myYongjin\"},{\"type\":\"miniprogram\",\"name\":\"我的下级\",\"key\":\"my_child\",\"url\":\"http://baidu.com\",\"appid\":\"wx631237a7c68b1660\",\"pagepath\":\"pages/myChild/myChild\"}]}]}";

//        String ticket = null;
        String errormsg = null;
        try {
            String requestUrl = gzh_create_menu_url.replace("ACCESS_TOKEN", access_token);
            JSONObject jsonObject = httpRequest(requestUrl, "POST", data);
            // 如果请求成功
            if (null != jsonObject) {
                try {
                    errormsg = jsonObject.getString("errmsg");
//                    url = jsonObject.getString("url");
                } catch (JSONException e) {
//                accessToken = null;
//                    ticket = null;
//                    url = null;
                    // 获取token失败
//                    log.error("获取token失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println(errormsg);
        return errormsg;
    }

    //删除菜单
    public static String deleteGzhMenu(String access_token) {
//        String data = "{\"button\":[{\"type\":\"click\",\"name\":\"我的二维码\",\"key\":\"my_qr_code\"}]}";

//        String ticket = null;
        String errormsg = null;
        try {
            String requestUrl = gzh_delete_menu_url.replace("ACCESS_TOKEN", access_token);
            JSONObject jsonObject = httpRequest(requestUrl, "POST", null);
            // 如果请求成功
            if (null != jsonObject) {
                try {
//                    ticket = jsonObject.getString("ticket");
                    errormsg = jsonObject.getString("errmsg");
                } catch (JSONException e) {
//                accessToken = null;
//                    ticket = null;
                    errormsg = null;
                    // 获取token失败
//                    log.error("获取token失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println(errormsg);

        return errormsg;
    }

    //------------------------获取用户信息
    //获取用户信息
    public static String getGzhUserInfo(String access_token, String gzhOpenId) {

        String errormsg = null;
        try {
            String requestUrl = gzh_get_user_info.replace("ACCESS_TOKEN", access_token).replace("OPEN_ID", gzhOpenId);
            JSONObject jsonObject = httpRequest(requestUrl, "POST", null);
            // 如果请求成功
            if (null != jsonObject) {
                try {
                    errormsg = jsonObject.getString("errmsg");
//                    errormsg = jsonObject.get("nickname").toString();
                } catch (JSONException e) {
                    errormsg = null;
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println(errormsg);

        return errormsg;
    }

    //---------------------二维码
    //获取公众号永久二维码
    public static String getGzhForeverQRCode(String access_token, String username) {
        String data = "{\"action_name\": \"QR_LIMIT_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": \"" + username + "\"}}}";

//        String ticket = null;
        String url = null;
        try {
            String requestUrl = gzh_rqcode_url_forever.replace("ACCESS_TOKEN", access_token);
            JSONObject jsonObject = httpRequest(requestUrl, "POST", data);
            // 如果请求成功
            if (null != jsonObject) {
                try {
//                    ticket = jsonObject.getString("ticket");
                    url = jsonObject.getString("url");
                } catch (JSONException e) {
//                accessToken = null;
//                    ticket = null;
                    url = null;
                    // 获取token失败
//                    log.error("获取token失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return url;
    }

    //获取小程序二维码
    public static String getXcxQrCode(String access_token, String page, String scene) {
//        String requestUrl = xcx_rq_code_url_temp.replace("ACCESS_TOKEN", access_token);
//
//
//        Map<String, Object> param = new HashMap<>();
//        param.put("page", page);
//        param.put("width", 360);
//        param.put("scene", scene);
//        param.put("auto_color", false);
//        Map<String, Object> line_color = new HashMap<>();
//        line_color.put("r", 0);
//        line_color.put("g", 0);
//        line_color.put("b", 0);
//        param.put("line_color", line_color);
//        param.put("is_hyaline", true);
//
//        JYFileUtils.FilePath fp = JYFileUtils.getFile(SYS_CONFIG_PATH.USER_IMAGE, DateUtils.getCurrTime() + ".png");
//        if (null == fp) {
//            return null;
//        }
//        String filePath = fp.fileFullPath;
//        httpRequest3(requestUrl, param, filePath);
//        return SYS_CONFIG_PATH.IMAGE_BASE_URL + fp.dbFileFullPath;

        String filePath = getXcxQrCodeFilePath(access_token, page, scene);
        if (ValidateUtil.isNull(filePath)) {
            return null;
        }
        return SYS_CONFIG_PATH.IMAGE_BASE_URL + filePath;
    }

    //获取小程序二维码
    public static String getXcxQrCodeFilePath(String access_token, String page, String scene) {
        String requestUrl = xcx_rq_code_url_temp.replace("ACCESS_TOKEN", access_token);

        Map<String, Object> param = new HashMap<>();
        param.put("page", page);
        param.put("width", 360);
        param.put("scene", scene);//上级账号
        param.put("auto_color", false);
        Map<String, Object> line_color = new HashMap<>();
        line_color.put("r", 0);
        line_color.put("g", 0);
        line_color.put("b", 0);
        param.put("line_color", line_color);
        param.put("is_hyaline", true);

        JYFileUtils.FilePath fp = JYFileUtils.getFile(SYS_CONFIG_PATH.USER_IMAGE, DateUtils.getCurrTime() + ".png");
        if (null == fp) {
            return null;
        }
        String filePath = fp.fileFullPath;
        httpRequest3(requestUrl, param, filePath);
        return fp.dbFileFullPath;
    }


    // 小程序-解密encryptedData敏感数据
    public static com.alibaba.fastjson.JSONObject decryptData(String encryptedData, String sessionKey, String iv) {
        // 被加密的数据
        byte[] dataByte = Base64.decode(encryptedData);
        // 加密秘钥
        byte[] keyByte = Base64.decode(sessionKey);
        // 偏移量
        byte[] ivByte = Base64.decode(iv);

        try {
            // 如果密钥不足16位，那么就补足.  这个if 中的内容很重要
            int base = 16;
            if (keyByte.length % base != 0) {
                int groups = keyByte.length / base + (keyByte.length % base != 0 ? 1 : 0);
                byte[] temp = new byte[groups * base];
                Arrays.fill(temp, (byte) 0);
                System.arraycopy(keyByte, 0, temp, 0, keyByte.length);
                keyByte = temp;
            }
            // 初始化
            Security.addProvider(new BouncyCastleProvider());
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
            SecretKeySpec spec = new SecretKeySpec(keyByte, "AES");
            AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES");
            parameters.init(new IvParameterSpec(ivByte));
            cipher.init(Cipher.DECRYPT_MODE, spec, parameters);// 初始化
            byte[] resultByte = cipher.doFinal(dataByte);
            if (null != resultByte && resultByte.length > 0) {
                String result = new String(resultByte, "UTF-8");
                return com.alibaba.fastjson.JSONObject.parseObject(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static void main(String[] args) throws Exception {

//        getUserOpenIDLittle("wxe419f5078b81fb51", "156c23c315d9cf6599cda72e463ee368", "071OVuV40oVarD1exoU40hKpV40OVuVK");

        String encryptedData = "raatvRzTkIqubHRzZy1r78sJ9QgvB5TUacUL8l7/oD2KLUJHF13Cf06mlFMHY186ld8iXF5SppOgtf2LKWrKlTZudFCfZmf1BNV3yaaelQUOz74tEvTFxp1AjaTdSEA7gvidYvSlTOl+bPiLtgEFlez/DrJk8sFBB4LDJFhzbGEUtTSut2GtGIRl+0gC0/1w53wYzJIyqIXi1j2Vib8VPg==";
        String sessionKey = "CvLSxDTAiBjxPQiqaZd7nA==";
        String iv = "25qyBkqVGRTuuNKnUVZxmA==";
        System.out.println(decryptData(encryptedData, sessionKey, iv));

//        String appId = WeixinConfig.gzhAppId;
//        String appSecret = WeixinConfig.gzhAppSecret;
//
//        System.out.println(getUserOpenIDLittle(appId, appSecret, "071BOZby0JvaNf1pSnay0FuMby0BOZbb"));
//        AccessToken at = WeixinUtil.getAccessToken(WeixinConfig.xcxAppId, WeixinConfig.xcxAppSecret);

//        createGzhMenu(at.getToken(), null);
//        deleteGzhMenu(at.getToken());
//
//        System.out.println(getUserOpenIDLittle(appId, appSecret, "071BOZby0JvaNf1pSnay0FuMby0BOZbb"));
//        getGzhUserInfo(at.getToken(), "o3CkM01T3xPGPPjhd72RZ_WaWl_U");


//        System.out.println(getXcxQrCode(at.getToken(), "pages/shareRegist/shareRegist", "15666371607"));

    }


    //---------------------------\

    // 菜单创建（POST） 限100（次/天）
//    public static String menu_create_url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";

//    /**
//     * 创建菜单
//     *
//     * @param menu 菜单实例
//     * @param accessToken 有效的access_token
//     * @return 0表示成功，其他值表示失败
//     */
//    public static int createMenu(Menu menu, String accessToken) {
//    	int result = 0;
//
//    	// 拼装创建菜单的url
//    	String url = menu_create_url.replace("ACCESS_TOKEN", accessToken);
//    	// 将菜单对象转换成json字符串
//    	String jsonMenu = JSONObject.fromObject(menu).toString();
//    	// 调用接口创建菜单
//    	JSONObject jsonObject = httpRequest(url, "POST", jsonMenu);
//
//    	if (null != jsonObject) {
//    		if (0 != jsonObject.getInt("errcode")) {
//    			result = jsonObject.getInt("errcode");
//    			log.error("创建菜单失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
//    		}
//    	}
//
//    	return result;
//    }
}
